<template>
  <div>
    <el-row type=flex>
      <el-col :span="16">
        <el-button @click="handleCreate" icon="el-icon-plus" type="primary">新建储值卡</el-button>
      </el-col>
      <el-col :span="8">
        <el-input placeholder="请输储值卡名称" v-model="filterForm.title">
          <el-button @click="handleFilter" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <el-table :data="balance.list" stripe class="hui-mt-1">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form :model="scope.row" label-width="8em" label-position="left" inline class="hui-data-table-expand">
            <el-form-item label="储值卡标题:">{{ scope.row.title }}</el-form-item>
            <el-form-item label="状态:">
              <el-tag v-if="scope.row.status === 0" size="small">正常</el-tag>
              <el-tag v-else-if="scope.row.status === 1" type="info" size="small">已删除</el-tag>
              <el-tag v-else type="danger" size="small">{{ scope.row.status }}</el-tag>
            </el-form-item>
            <el-form-item label="面额:">{{ scope.row.balance / 100 | currency('￥', 2) }}</el-form-item>
            <el-form-item label="售价:">{{ scope.row.amount / 100 | currency('￥', 2) }}</el-form-item>
            <el-form-item label="适用门店:">{{ scope.row.useAllBranch ? '所有门店' : '部分门店' }}</el-form-item>
            <el-form-item v-if="!scope.row.useAllBranch" label="适用门店列表:">
              <el-tag v-for="item in scope.row.canUseBranch" :key="item.branchId" size="small" type="primary" class="hui-mr-8px">{{ item.branchName }}</el-tag>
            </el-form-item>
            <el-form-item label="续充设置:">
              <template v-if="scope.row.reSave">可以续充</template>
              <template v-else>不可续充</template>
            </el-form-item>
            <el-form-item label="礼品卡:">
              <template v-if="scope.row. isGiftCard">是</template>
              <template v-else>否</template>
            </el-form-item>
            <el-form-item v-if="scope.row.presentCard" label="赠送会员卡:">{{ scope.row.presentCard.title }}</el-form-item>
            <el-form-item v-if="scope.row.reward" label="储值赠送奖励:">
              <el-table :data="scope.row.reward" stripe border size="small" style="width: 320px; line-height: 23px;">
                <el-table-column label="标题" prop="title"></el-table-column>
                <el-table-column label="数量" prop="number"></el-table-column>
              </el-table>
            </el-form-item>
            <el-form-item label="用户打标签:">
              <el-tag v-for="item in scope.row.userTags" :key="item.id" size="small" class="hui-mr-8px"># {{ item.label }} #</el-tag>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="储值卡标题 / 门店 / 状态">
        <template slot-scope="scope">
          <div>{{ scope.row.title }}</div>
          <div>{{ scope.row.useAllBranch ? '所有门店' : '适用&nbsp;' + scope.row.canUseBranch.length + '&nbsp;门店' }}</div>
          <div>
            <el-tag v-if="scope.row.status === 0" size="small">正常</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="info" size="small">已删除</el-tag>
            <el-tag v-else type="danger" size="small">{{ scope.row.status }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="储值卡金额">
        <template slot-scope="scope">
          <div>面额: {{ scope.row.balance / 100 | currency('￥', 2) }}</div>
          <div>售价: {{ scope.row.amount / 100 | currency('￥', 2) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="最后操作人">
        <template slot-scope="scope">
          <div>{{ scope.row.updateUser }}</div>
          <div>{{ $moment.unix(scope.row.updateTime).format("YYYY-MM-DD HH:mm:ss") }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="240px">
        <template slot-scope="scope">
          <el-button @click="handleCopy(scope.row)" :disabled="scope.row.status === 0" type="success" size="mini" plain>链接</el-button>
          <el-button @click="handleEdit(scope.row)" :disabled="scope.row.status === 0" type="primary" size="mini" plain>编辑</el-button>
          <el-button @click="handleDelete(scope.row)" :disabled="scope.row.status === 0" type="danger" size="mini" plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页，start-->
    <el-pagination @current-change="handleCurrentChange" :current-page.sync="balance.pages" :page-sizes="[10]" :page-size="10" layout="total, sizes, prev, pager, next" :total="balance.total" background class="hui-mt-2"></el-pagination>
    <!--分页，end-->
  </div>
</template>

<script>
export default {
  data () {
    return {
      filterForm: {
        title: ''
      },
      loading: false,
      balance: {
        pages: 0,
        total: 0,
        list: []
      }
    }
  },
  methods: {
    // 筛选事件
    handleFilter () {
      if (this.filterForm.title === '') {
        return false
      } else {
        this.loading = true
        console.log(this.filterForm)
        this.$loading()
        setTimeout(() => {
          this.$loading().close()
          this.loading = false
        }, 500)
      }
    },
    // 新建事件
    handleCreate () {
      this.$router.push({
        path: 'detail',
        query: {
          type: 'create'
        }
      })
    },
    // 复制链接
    handleCopy (item) {
      this.$confirm(item.balanceUrl, '储值链接', {
        confirmButtonText: '复制',
        cancelButtonText: '取消',
        type: 'success'
      }).then(() => {
        this.$copyText(item.balanceUrl).then(res => {
          console.log('复制成功:' + res)
          item.visibleClipboard = false
          this.$notify({
            title: '复制成功',
            message: '可将链接粘贴到想用的地方',
            type: 'success',
            duration: 1500
          })
        }).catch(err => {
          console.log('复制失败:' + err)
          this.$notify({
            title: '复制失败',
            message: '请选择活动链接，手工 Ctrl + C 进行复制',
            type: 'warning',
            duration: 3000
          })
        })
      }).catch(() => {})
    },
    // 编辑事件
    handleEdit (row) {
      let id = row.id
      this.$router.push({
        path: 'detail',
        query: {
          type: 'edit',
          id: id
        }
      })
    },
    // 删除事件
    handleDelete (row) {
      this.$confirm('确认删除这个储值卡么？删除后，本储值卡已经发给用户的，仍可继续使用，但但用户无法再购买本储值卡。', '删除确认', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('删除, ID:' + row.id)
        this.$message({
          type: 'success',
          message: '已删除!'
        })
      }).catch(() => {})
    },
    // 分页事件
    handleCurrentChange () {
      console.log(this.balance.pages)
      this.$loading()
      setTimeout(() => {
        this.$loading().close()
      }, 500)
    },
    // 数据模拟
    mockData () {
      let pages = this.balance.pages
      let total = 100
      let list = this.balance.list
      let i = 10
      while (i > 0) {
        list.push({
          id: i,
          status: this.$randomNumber(0, 1),
          updateTime: parseInt(this.$moment().format('X')),
          updateUser: '黄倬, 13262268525',
          title: '储值卡标题' + i,
          reSave: i % 2 !== 0,
          balance: this.$randomEnum([30000]),
          amount: 24900,
          balanceUrl: 'http://qq.com',
          isGiftCard: i % 2 !== 0,
          useAllBranch: i % 2 !== 0,
          canUseBranch: i % 2 !== 0 ? [] : [{branchId: i, branchName: '门店名称' + i}, {branchId: i + 1, branchName: '门店名称' + (i + 1)}],
          presentCard: {
            cardId: i,
            title: '会员卡名称' + i
          },
          reward: [
            {
              cardId: '1',
              title: '优惠券标题1',
              number: 1
            },
            {
              cardId: '2',
              title: '优惠券标题2',
              number: 1
            }
          ],
          userTags: i % 2 !== 0 ? [] : [{id: i, label: '标签名称' + i}, {id: i + 1, label: '标签名称' + (i + 1)}]
        })
        i--
      }
      this.balance.total = total
      this.balance.pages = pages + 1
      this.balance.list = list
    }
  },
  created () {
    this.mockData()
  }
}
</script>
